import Taro from "@tarojs/taro";
import { Text, View ,Button,Image} from "@tarojs/components";
import { useState,useEffect} from "react";
import { AtIcon, } from 'taro-ui'
import { submitResume,addFavoritePosition } from "../../api/mode";
import bigHouse from '../../images/bigHouse.png'
import './index.scss'



export default function PostCard(props){
//postCard接收职位信息(Info)和是否为专场招聘(isDetail)和是否收藏（isCollected）三个信息  
const [submitIsOpen,setSubmitIsOpen]=useState(false)
const [favoriteIsOpen,setFavoriteIsOpen]=useState(false)//收藏成功弹窗
const [isCollected,setIsCollected]=useState(props.isCollected)//收藏的星标
const isDetail=props.isDetail?props.isDetail:false//判断是投递简历还是查看详情
const openid=Taro.getStorageSync(`openid`)
const [Info,setInfo]=useState(props.Info);
const companyId=Info.companyId
const positionId=Info.id

// console.log(positionId,isCollected);
// const Qs=require('qs')
// const chooseCompany=(id)=>{
//   switch(id){
//     case "0": return('国企')
//     case "1": return('央企')
//     case "2": return('事业单位')
//     case "3": return('政府机关')
//     case "4": return('民营企业')

//   }
// }

useEffect(//在切换按薪资和按时间时重新渲染子组件
  ()=>{
    setInfo(props.Info)
  }
  ,[props.Info])

useEffect(
  ()=>{
    // console.log(props.isCollected,isCollected,props.Info.id);
    setIsCollected(props.isCollected)
    }
  ,[props.isCollected,props.Info.id])//传入是否收藏和该次的id，使其可以在切换的时候实时刷新收藏按钮



const submitItem={
  openid:openid,
  companyId:companyId,
  positionId:positionId
}

const submitMyResume=(e)=>{
  // console.log(e,"resume")
  e.stopPropagation()
  if(Taro.getStorageSync(`openid`)){
    submitResume(submitItem).then(res=>{
      console.log('提交成功',res);
      setSubmitIsOpen(!submitIsOpen)
      if(res.data.code===600){
        Taro.showToast({
          title:'投递成功',
          icon:'success',
          duration:1000
        })
      }else if(res.data.code===602){
          if(res.data.message=='请先完善简历信息,然后进行简历投递!'){
            Taro.showToast({
              title:'请先完善简历',
              icon:'error',
              duration:1000
            })
          }else{
            Taro.showToast({
              title:'请勿重复投递',
              icon:'error',
              duration:1000
            })
          }
        
      }
    })
  }else{
    Taro.showToast({
      title: '请先登录',
    icon: 'error',
    duration: 1500
    })
  }

}

const submitMyFavorite=(e)=>{
  // console.log(e,e[0],"favorite")
  e[0].stopPropagation()
if(Taro.getStorageSync(`openid`)){
  addFavoritePosition(submitItem).then(res=>{
    console.log("change collect",res);
      setIsCollected(!isCollected)//改变收藏图标
      if(!isCollected){
        Taro.showToast({
          title:'收藏成功',
          icon:'success',
          duration:1000
        })
      }
      // setFavoriteIsOpen(!favoriteIsOpen)
      // console.log(res);
    // if(!isCollected){
    //   console.log('faboriet==',favoriteIsOpen);
    // }
})
}else{
  Taro.showToast({
    title: '请先登录',
  icon: 'error',
  duration: 1500
  })
}
}


//点击卡片时的跳转
const jumpToDetail=()=>{
  if(props.canJump){//避免多次跳转
    Taro.navigateTo({
      url:`/pages/positionDetail/positionDetail?positionId=${positionId}`
    })
  } 
}
//点击卡片查看详情按钮时的跳转
const jumpToDetailButton=()=>{
    Taro.navigateTo({
      url:`/pages/positionDetail/positionDetail?positionId=${positionId}`
    })
}

// const handleCardClick=(judge)=>{
//   if(!judge) jumpToDetail()
// }

  return (
    <View className='postCard' onClick={jumpToDetail} >
      <View >
        <View className='positionName'>{Info.positionName}</View>
         <View className='sal'>{Info.salary.minSal}-{Info.salary.maxSal}千/月</View>
      </View>

      <View className='positionRequest'>
      <Text>{Info.positionRequest.address} | </Text>
      <Text>{Info.positionRequest.eduDegree} | </Text>
      <Text>{Info.positionRequest.workExp}</Text>
      <Text className='time'>{Info.publishTime}</Text>
      </View>

      <View className='company'>
      {/* <AtIcon value='home' size='20' color='#000'></AtIcon> */}
      <View className='imgBox'> <Image className='bigHouse' src={bigHouse}></Image></View>
      <Text>{Info.companyName}</Text>
      <Text className='companyType'>{Info.companyType}</Text>
      </View>

      <View className='feature at-row at-row--wrap'>
          {Info.feature? Info.feature
          .sort((a,b)=>{
            if(a.length<b.length) return -1
            else return 1
          })
          .map((text,index)=>{
            if(text.length<=4){
              return (<View className='feature_item '  key={index}><Text >{text}</Text></View>)
            }
            else {
             return  (<View className='feature_item '   key={index}><Text >{text}</Text></View>)
            }
          })
          
          
          :null}
      </View>
     
      <View className='bottom'>

     {/* 收藏按钮 */}
     <AtIcon  value={isCollected?'star-2':'star'} size='30' color='#4C80AE' onClick={submitMyFavorite}></AtIcon>

     {isCollected
     ?
     <AtIcon  value='star-2' size='40' color='white' onClick={submitMyFavorite}></AtIcon>
     :
     <AtIcon  value='star' size='40' color='white' onClick={submitMyFavorite}></AtIcon>
     }
      {/* <AtIcon  value={isCollected?'star-2':'star'} size='30' color='#000' onClick={submitMyFavorite}></AtIcon> */}
    
    
    {/* 判断是投递简历还是查看详情 */}
      {   isDetail
      ?<Button className='postitionButton' onClick={jumpToDetailButton}>查看详情</Button>
      :<Button className='postitionButton' onClick={submitMyResume}>一键投递</Button>
    }


      {/* <AtToast  isOpened={submitIsOpen} text='投递成功' icon='check'></AtToast> */}
      {/* <AtToast  isOpened={favoriteIsOpen} text='收藏成功' icon='check'></AtToast> */}
      </View>
    </View>

    
  )
}